﻿#include "userhomepage.h"
#include "ui_userhomepage.h"
#include <QMessageBox>
#include <QPixmap>
#include <QDebug>
#include <QDate>
#include <QFileDialog>
#include <QDir>

UserHomePage::UserHomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserHomePage)
{
    ui->setupUi(this);

    m_uploadCount = 0;
    m_isOkToModifyPswd = false;
    m_isOkToModifyInfos = false;
    m_imageVector.reserve(2);
    m_imageVector.append("");
    m_imageVector.append("");
    ui->pb_submit->setEnabled(false);
    ui->tabw_main->tabBar()->setStyle(new CustomTabStyle);//注意，设置上述代码风格 就可以实现QTabBar横向
    ui->tabw_main->setCurrentIndex(0);
}

UserHomePage::~UserHomePage()
{
    delete ui;
}

void UserHomePage::initUserInfos()
{
    if (GlobalVars::g_localUser.getRole().toInt() != USER_Business)
    {
        ui->tabw_main->removeTab(2);
    }
    ui->le_name->setText(GlobalVars::g_localUser.getName());
    ui->lb_nameB->setText(GlobalVars::g_localUser.getName());
    ui->le_email->setText(GlobalVars::g_localUser.getEmail());
    ui->le_phone->setText(GlobalVars::g_localUser.getPhone());
    ui->cb_sex->setCurrentText(GlobalVars::g_localUser.getSex());
    ui->de_birth->setDate(QDate::fromString(GlobalVars::g_localUser.getBirthday(), "yyyy/MM/dd"));

    ui->le_realName->setText(GlobalVars::g_localUser.getRealName());
    QString certificate = GlobalVars::g_localUser.getCertificate();
    ui->lb_certification->setText("");
    if (certificate != "未认证")
    {
        if (certificate == "已认证")
        {
            ui->lb_realName->show();
            ui->le_realName->show();
            ui->le_realName->setEnabled(false);
            ui->lb_certification->setStyleSheet(
                        "min-width: 180px; max-width: 180px;min-height: 150px;max-height: 150px;"
                        "border-image: url(:/images/logo/certificated.jpg);");
        }
        else
        {
            ui->lb_realName->close();
            ui->le_realName->close();
            ui->lb_certification->setStyleSheet(
                        "min-height: 60px;max-height: 60px;"
                        "border-image: url(:/images/logo/certificating.jpg);");
        }
        ui->pb_upload1->close();
        ui->pb_upload2->close();
        ui->lb_upload1->close();
        ui->lb_upload2->close();
        ui->pb_submit->close();
    }
    else
    {
        ui->lb_realName->show();
        ui->le_realName->show();
        ui->le_realName->setEnabled(true);
        ui->lb_certification->setStyleSheet(
                    "min-width: 180px; max-width: 180px;min-height: 180px;max-height: 180px;"
                    "border-image: url(:/images/logo/no_certificated.jpg);");
    }
}

void UserHomePage::FailGainInfos(const QString &title, const QString &msg)
{
    QMessageBox::warning(this,
                 title,
                 msg,
                 QMessageBox::Ok,
                 QMessageBox::Ok);
}

void UserHomePage::setHeadPic(const QImage& image)
{
    QPixmap pixMap = QPixmap::fromImage(image.scaled(100,100, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
    pixMap = GlobalVars::PixmapToRound(pixMap, 50); ///30为圆形的半径
    ui->lb_headPic->setPixmap(pixMap);
}

void UserHomePage::businessManCertificateResult()
{
    m_uploadCount++;
    if (m_uploadCount >= 2)
    {
        ui->lb_certification->setText("");///上传成功，等待认证...
    }
    else{
        ui->lb_certification->setText(QString("上传中...  (%1/2)").arg(QString::number(m_uploadCount)));
    }
}

void UserHomePage::on_pb_modifyHeadPic_clicked()
{
    QString m_fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
                                                            "F:\\ForWork\\HotelPlatform\\image\\head_pic\\avatar",
                                                            tr("Image (*.png *.jpg)"));

    if (m_fileName.isEmpty())
    {
        return;
    }

    emit signalModifyHeadPic(m_fileName);
}

void UserHomePage::on_pb_modifyPassword_clicked()
{
    if (m_isOkToModifyPswd)
    {
        QString passwordOld = ui->le_password->text();
        QString passwordNew = ui->le_newPassword->text();
        ui->le_password->clear();
        ui->le_newPassword->clear();
        ui->le_newPasswordOK->clear();

        emit signalModifyPassword(passwordOld,passwordNew);
    }
}

void UserHomePage::on_pb_modifyPhone_clicked()
{
    if (ui->pb_modifyPhone->text() == "确定")
    {
        if (checkModifiedPhone())
        {
            ui->le_phone->setEnabled(false);
            ui->pb_finish->setEnabled(true);
            ui->pb_modifyPhone->setText("更改号码");
        }
        else
        {
            FailGainInfos("错误", "格式不正确");
        }
    }else
    {
        ui->le_phone->setEnabled(true);
        ui->pb_finish->setEnabled(false);
        ui->pb_modifyPhone->setText("确定");
    }
}

void UserHomePage::on_pb_modifyEmail_clicked()
{
    if (ui->pb_modifyEmail->text() == "确定")
    {
        if (checkModifiedEmail())
        {
            ui->le_email->setEnabled(false);
            ui->pb_finish->setEnabled(true);
            ui->pb_modifyEmail->setText("更改邮箱");
        }
        else
        {
            qDebug() << "error";
            FailGainInfos("错误", "格式不正确");
        }
    }else
    {
        ui->le_email->setEnabled(true);
        ui->pb_finish->setEnabled(false);
        ui->pb_modifyEmail->setText("确定");
    }
}

void UserHomePage::on_pb_finish_clicked()
{
    bool isChange = false;

    QStringList msg;
    if (ui->le_name->text() != GlobalVars::g_localUser.getName())
    {
        msg.append(ui->le_name->text());
        isChange = true;
    }else
    {
        msg.append("");
    }

    if (ui->cb_sex->currentText() != GlobalVars::g_localUser.getSex())
    {
        msg.append(ui->cb_sex->currentText());
        isChange = true;
    }else
    {
        msg.append("");
    }

    if (ui->de_birth->text() != GlobalVars::g_localUser.getBirthday())
    {
        msg.append(ui->de_birth->text());
        isChange = true;
    }else
    {
        msg.append("");
    }

    if (ui->le_phone->text() != GlobalVars::g_localUser.getPhone())
    {
        msg.append(ui->le_phone->text());
        isChange = true;
    }else
    {
        msg.append("");
    }

    if (ui->le_email->text() != GlobalVars::g_localUser.getEmail())
    {
        msg.append(ui->le_email->text());
        isChange = true;
    }else
    {
        msg.append("");
    }

    if (isChange)
    {
        emit signalUpdateUserInfos(msg);
    }
}

void UserHomePage::on_pb_upload1_clicked()
{
    QString m_fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
                                                            "F:\\ForWork\\HotelPlatform\\image\\head_pic\\avatar",
                                                            tr("Image (*.png *.jpg)"));

    if (m_fileName.isEmpty())
    {
        return;
    }
    QImage image(m_fileName);
    m_imageVector[0] = m_fileName;
    if (image.width() > image.height())
    {
        ui->lb_upload1->setPixmap(QPixmap::fromImage(image.scaled(image.width() * 150 / image.height(),150)));
    }
    else{
        ui->lb_upload1->setPixmap(QPixmap::fromImage(image.scaled(150,image.height() * 150 / image.width())));
    }


    if (!m_imageVector[1].isEmpty())
    {
        ui->pb_submit->setEnabled(true);
    }
}

void UserHomePage::on_pb_upload2_clicked()
{
    QString m_fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
                                                            "F:\\ForWork\\HotelPlatform\\image\\head_pic\\avatar",
                                                            tr("Image (*.png *.jpg)"));

    if (m_fileName.isEmpty())
    {
        return;
    }
    QImage image(m_fileName);
    m_imageVector[1] = m_fileName;
    if (image.width() > image.height())
    {
        ui->lb_upload2->setPixmap(QPixmap::fromImage(image.scaled(image.width() * 150 / image.height(),150)));
    }
    else{
        ui->lb_upload2->setPixmap(QPixmap::fromImage(image.scaled(150,image.height() * 150 / image.width())));
    }

    if (!m_imageVector[0].isEmpty())
    {
        ui->pb_submit->setEnabled(true);
    }
}


void UserHomePage::on_pb_submit_clicked()
{
    if (m_imageVector[1].isEmpty() || m_imageVector[0].isEmpty())
    {
        return;
    }

    ui->lb_certification->setStyleSheet("");
    ui->lb_certification->setText("上传中...  (0/2)");
    QString realName = ui->le_realName->text();
    QStringList imageList;
    imageList << realName << m_imageVector[0] << m_imageVector[1] ;
    emit signalBusinessManCertificate(realName, imageList);
}



void UserHomePage::on_le_newPassword_editingFinished()
{
    QString arg1 = ui->le_newPassword->text();
    if (!arg1.isEmpty())
    {
        QRegExp password_reg_exp("^[A-Za-z0-9]{6,20}$");
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            ui->lb_passworOK2->setText("格式不正确");
        }
        else
        {
            if (arg1 == ui->le_password->text())
            {
                ui->lb_passworOK2->setText("不能与旧密码相同");
            }
            ui->lb_passworOK2->setText(" ");
        }
    }
    else{
        ui->lb_passworOK2->setText(" ");
    }
}

///匹配英文字符+数字 6-20
void UserHomePage::on_le_password_editingFinished()
{
    QString arg1 = ui->le_password->text();
    if (!arg1.isEmpty())
    {
        QRegExp password_reg_exp("^[A-Za-z0-9]{6,20}$");
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            ui->lb_passworOK1->setText("格式不正确");
            m_isOkToModifyPswd = false;
        }
        else
        {
            ui->lb_passworOK1->setText(" ");
        }
    }
    else{
        m_isOkToModifyPswd = false;
        ui->lb_passworOK1->setText(" ");
    }
}

void UserHomePage::on_le_newPasswordOK_textChanged(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        if(arg1 != ui->le_newPassword->text())/// 匹配不对
        {
            m_isOkToModifyPswd = false;
            ui->lb_passworOK3->setText("两次输入密码不一致");
        }
        else
        {
            if (!ui->le_password->text().isEmpty())
            {
                m_isOkToModifyPswd = true;
                ui->lb_passworOK3->setText("√");
            }
        }
    }
    else
    {
        ui->lb_passworOK3->setText(" ");
    }
}

bool UserHomePage::checkModifiedPhone()
{
    QString arg1 = ui->le_phone->text();
    if (!arg1.isEmpty())
    {
        QRegExp password_reg_exp("^13[0-9]{1}[0-9]{8}|^15[0-9]{1}[0-9]{8}|^18[0-9]{1}[0-9]{8}");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    return false;
}

bool UserHomePage::checkModifiedEmail()
{
    QString arg1 = ui->le_email->text();
    if (!arg1.isEmpty())
    {
        QRegExp password_reg_exp("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    return false;
}

void UserHomePage::on_le_name_editingFinished()
{
//
    QString arg1 = ui->le_name->text();
    if (!arg1.isEmpty())
    {
        QRegExp reg_exp("^[\u4e00-\u9fa5]|\\w+$");///
        QRegExpValidator *validator = new QRegExpValidator(reg_exp);
        if(!validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_isOkToModifyInfos = false;
            ui->lb_nameRemind->setText("含有非法字符");
        }
        else
        {
            m_isOkToModifyInfos = true;
            ui->lb_nameRemind->setText(" ");
        }
    }
}
